<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="0px" class="top-left">
      <el-form-item label prop="learnType">
        <el-select v-model="queryParams.learnType" placeholder="请选择培训种类" clearable>
          <el-option v-for="dict in dict.type.training_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label prop="learnDate">
        <el-date-picker clearable v-model="queryParams.learnDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择培训日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['dept:bsLearn:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['dept:bsLearn:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['dept:bsLearn:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['dept:bsLearn:export']">导出</el-button>
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table border stripe class="table" v-loading="loading" :data="bsLearnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="80">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column label="培训内容" align="center" prop="learnContent" min-width="160" show-overflow-tooltip />
      <el-table-column label="培训类别" align="center" prop="learnTypeStr" width="160" show-overflow-tooltip />
      <el-table-column label="培训日期" align="center" prop="learnDate" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.learnDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="培训地点" align="center" prop="learnAddress" min-width="160" show-overflow-tooltip />
      <el-table-column label="授课单位(老师)" align="center" prop="lecturer" width="160" show-overflow-tooltip />
      <el-table-column label="参加培训人员" align="center" prop="trainers" min-width="160" show-overflow-tooltip />
      <el-table-column label="培训效果" align="center" prop="trainingEffect" min-width="160" show-overflow-tooltip />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['dept:bsLearn:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['dept:bsLearn:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改试验室培训对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="培训内容" prop="learnContent">
              <el-input v-model="form.learnContent" placeholder="请输入培训内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训类别" prop="learnType">
              <el-select v-model="form.learnType" placeholder="请选择培训种类" style="width: 100%">
                <el-option v-for="dict in dict.type.training_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="培训日期" prop="learnDate">
              <el-date-picker clearable v-model="form.learnDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择培训日期" style="width: 100%"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训地点" prop="learnAddress">
              <el-input v-model="form.learnAddress" placeholder="请输入培训地点" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="授课单位(老师)" prop="lecturer" class="narrow-spacing">
              <el-input v-model="form.lecturer" placeholder="请输入授课单位(老师)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训效果" prop="trainingEffect">
              <el-select v-model="form.trainingEffect" placeholder="请选择培训效果" style="width: 100%">
                <el-option v-for="e in trainingEffects" :key="e.value" :label="e.label" :value="e.value" />
              </el-select>
              <!-- <el-input v-model="form.trainingEffect" placeholder="请选择培训效果" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="文件" prop="syspx">
              <file-upload v-model="form.syspx" v-if="title == '修改试验室培训'" />
              <file-upload v-else v-model="form.syspx" />
              <!-- <fileUpload v-model="form.syspx"  /> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参加培训人员" prop="trainers" class="narrow-spacing">
              <el-input v-model="form.trainers" placeholder="请输入参加培训人员" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listBsLearn, getBsLearn, delBsLearn, addBsLearn, updateBsLearn } from "@/api/dept/bsLearn";
import {mapGetters} from "vuex";

export default {
  name: "BsLearn",
  dicts: ["training_type"],
  data() {
    return {
      trainingEffects: [
        { label: "良好", value: "良好" },
        { label: "较好", value: "较好" },
        { label: "一般", value: "一般" },
        { label: "较差", value: "较差" }
      ],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 试验室培训表格数据
      bsLearnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        learnId: undefined,
        deptId: undefined,
        learnType: undefined,
        learnDate: undefined,
        learnAddress: undefined,
        learnStatus: undefined
      },
      // 表单参数
      form: {
        syspx: null
      },
      // 表单校验
      rules: {
        // deptId: [
        //   { required: true, message: "试验室id不能为空", trigger: "blur" }
        // ],
        // learnType: [
        //   { required: true, message: "培训种类不能为空", trigger: "change" }
        // ],
        // learnDate: [
        //   { required: true, message: "培训日期不能为空", trigger: "blur" }
        // ],
        // learnAddress: [
        //   { required: true, message: "培训地点不能为空", trigger: "blur" }
        // ],
        // learnStatus: [
        //   { required: true, message: "培训状态不能为空", trigger: "change" }
        // ],
      }
    };
  },
  watch: {
    deptId (newValue, oldValue) {
      this.queryParams.deptId = newValue;
      this.getList();
    }
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["deptId", "userDeptId"])
  },
  created() {
    this.queryParams.deptId = this.$store.state.dept.deptId;
    this.getList();
  },
  methods: {
    /** 查询试验室培训列表 */
    getList() {
      this.loading = true;
      listBsLearn(this.queryParams).then((response) => {
        this.bsLearnList = response.rows;
        this.bsLearnList.map((e) => {
          for (let i of this.dict.type.training_type) {
            if (i.value == e.learnType) {
              e.learnTypeStr = i.label;
            }
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        learnId: undefined,
        deptId: undefined,
        learnType: undefined,
        learnDate: undefined,
        learnAddress: undefined,
        learnStatus: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        syspx:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.learnId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加试验室培训";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const learnId = row.learnId || this.ids;
      getBsLearn(learnId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改试验室培训";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.learnId != null) {
            updateBsLearn(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBsLearn(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const learnIds = row.learnId || this.ids;
      this.$modal
        .confirm('是否确认删除试验室培训编号为"' + learnIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delBsLearn(learnIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dept/bsLearn/export",
        {
          ...this.queryParams
        },
        `bsLearn_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
